<!--
 * @Description: 海报设置列表
 * @Author: Li Yujie
 * @Date: 2020/12/22
 * @Last Modified by: Li Yujie
 * @Last Modified time: 2020/12/22
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    海报设置
                </div>
                <div class="add-btn">
                    <el-button
                        type="primary"
                        icon="el-icon-plus"
                        @click="toEdit('', 'add')"
                    >
                        新增Banner
                    </el-button>
                </div>
            </div>
            <!-- /header -->
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">Banner名称：</p>
                    <el-input
                        placeholder="请输入banner名称"
                        v-model="filterData.keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="small"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                </div>
            </div>
            <!--筛选-->
            <!-- table -->
            <el-table
                :data="tableData"
                height="100%"
                style="width: 100%;"
            >
                <el-table-column
                    label="序号"
                    type="index"
                    align="center"
                    :formatter="emptyFormatter"
                    width="100"
                />
                <el-table-column
                    prop="banner_title"
                    label="banner名称"
                    align="center"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="banner_no"
                    label="banner编号"
                    align="center"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop=""
                    label="状态"
                    align="center"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <p v-if="scope.row.show_status === 0">已隐藏</p>
                        <p v-if="scope.row.show_status === 1">已显示</p>
                    </template>
                </el-table-column>
                <el-table-column
                    label="上架状态"
                    align="center"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <el-switch
                            v-model="scope.row.banner_status"
                            :active-value="1"
                            :inactive-value="0"
                            @change="handleModify(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column
                    label="开始时间"
                    show-overflow-tooltip
                    align="center"
                >
                    <template slot-scope="scope">
                        {{ scope.row.start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="结束时间"
                    show-overflow-tooltip
                    align="center"
                >
                    <template slot-scope="scope">
                        {{ scope.row.end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="left"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="toEdit(scope.row.banner_no, 'edit')"
                        >
                            编辑
                        </el-button>
                        <el-button
                            type="text"
                            @click="handleDelete(scope.row.banner_no)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- /table -->

            <!-- 分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    :current-page.sync="page.now_page"
                    background
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
            <!-- /分页 -->
        </div>
        <!-- 图片预览 -->
        <img-preview ref="preview"/>
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import {back} from '@/mixin/back';

export default {
    name: 'MemberPosterList',
    mixins: [back],
    data() {
        return {
            filterData: {
                keyword: ''
            },
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dateTimeRange: [],
            authResult: {}
        };
    },
    components: {
        imgPreview,
    },
    mounted() {
        this.init();
    },
    watch: {
        dateTimeRange(value) {
            this.filterData.create_start_time = value ? value[0] / 1000 : '';
            this.filterData.create_end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        }
    },
    methods: {
        /**
         * 初始化
         */
        init() {
            this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {
                title: '会员卡管理',
                path: '/student/member-card-list'
            }, {title: '海报设置'}]);
            this.authResult = this.$route.meta.authResult;
            this.getTableData();
        },

        /**
         * @description 获取会员卡管理列表数据
         * @param page
         */
        getTableData(page) {

            let params = {...this.filterData};

            params.page_id = page || this.page.now_page;

            this.$post('/student/shop_vip_banner%5Cget_list', params, resp => {
                if (resp.code === 1) {
                    this.tableData = resp.data.list;
                    this.page = resp.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },
        /**
         * 表格数据过滤函数
         * @param row
         * @param column
         * @param val
         * @returns {string|*}
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 跳转编辑页面
         * @param no
         * @param type - add新增 edit编辑 detail查看
         */
        toEdit(no = '', type) {

            this.$router.push({
                name: 'memberPosterEdit',
                query: {
                    no,
                    type
                }
            });
        },

        /**
         * 删除操作
         * @param no
         */
        handleDelete(no) {
            if (!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$confirm('此操作将删除该banner, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    material_no: no
                };

                this.$post('/student/shop_vip_banner%5Cdelete', params, resp => {
                    if (resp.code === 1) {
                        this.$notify({
                            title: '成功',
                            message: '删除成功',
                            duration: 2000,
                            type: 'success'
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            });
        },

        /**
         * 上架下架
         * @param row
         */
        handleModify(row) {
            if (!this.authResult.auth_modify) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$post('/student/shop_vip_banner%5Cmodify', {
                banner_no: row.banner_no,
                banner_status: row.banner_status
            }, resp => {
                if (resp.code === 1) {
                    this.$notify({
                        title: '成功',
                        message: '操作成功',
                        duration: 2000,
                        type: 'success'
                    });
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 预览图片
         * @param   {String}    images      //单图预览
         * @param   {Array}     images      //多图预览
         * @param index
         */
        fnShowPreview(images, index) {
            if (images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images, index);
            } else {
                this.$message({
                    type: 'warning',
                    message: '图片类型错误'
                });
            }
        },
    }
};
</script>

<style scoped lang="less">
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        /*筛选*/

        .filter-view {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            padding: 24px 24px 0;

            .header-new-box {
                display: flex;
                align-items: center;
                color: #606266;
                white-space: nowrap;
                margin-right: 32px;
                margin-bottom: 24px;

                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding: 10px 32px;

            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }


    }
}
</style>
